<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-04 11:45:41
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-27 19:14:35
 */
namespace app\admin\controller;
use app\__MODULE__\model\Banner as BannerModel;
use app\common\BaseController;

// banner管理模块
class Banner extends BaseController
{


    /**
     * banner列表
     * date 2020/8/13 17:54
     */
    public function index() :\think\response\Json{
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $wheres = [];
        if(isset($search['position']) && $search['position'] !=''){
            $wheres[] = ['position','=',$search['position']];
        }
        if(isset($search['type']) && $search['type'] !=''){
            $wheres[] = ['type','=',$search['type']];
        }
        if(isset($search['name']) && $search['name'] !=''){
            $wheres[] = ['name','like',"%{$search['name']}%"];
        }
        $query  = BannerModel::where($wheres);
        $total  = $query->count();
        $data   = $query->page($page, $limit)->order('sort desc')->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
        ]);
    }

    /**
     * 上架/下架操作
     * date 2021/3/17 11:20
     */
    public function is_show() :\think\response\Json{
        $id      = param_check('id');
        $checked = param_check('checked', 0);
        $res = BannerModel::where('id', $id)->update([
            'status'        => $checked ? 1 : 0,
            'update_time'   => time()
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }

    /**
     * 详情
     * @param int $id
     * @return \think\response\Json
     * @author Sam
     * @date 2022-04-26
     */
    public function detail(int $id): \think\response\Json
    {
        try {
            $row  = BannerModel::where('id', $id)->find();
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return $this->error($e->getMessage());
        }

        return $this->success('', $row);
    }
    /**
     * 添加banner
     * date 2020/8/13 17:54
     */
    public function create() :\think\response\Json {
        if(request()->isPost()) {
            $data = request()->post();
            try {
                $this->validate($data, [
                    'position' => 'require',
                    'name' => 'require',
                    'image' => 'require',
                ], [
                   'position.require'  => '位置必须',
                   'name.require'      => '名称必须',
                   'image.require'      => '图片必须',
                ]);
            } catch (\Exception $e) {
                json_response(0,$e->getError());
            }
            if(BannerModel::create($data)) json_response(1,'操作成功');
            json_response(0,'操作失败');
         }
    }

    /**
     * 编辑banner
     * date 2020/8/13 17:55
     */
    public function edit():\think\response\Json {
            $data = request()->post();
            try {
                $this->validate($data, [
                    'id' => 'require',
                    'position' => 'require',
                    'name' => 'require',
                    'image' => 'require',
                ], [
                   'id.require'  => 'ID必须',
                   'position.require'  => '位置必须',
                   'name.require'      => '名称必须',
                   'image.require'      => '图片必须',
                ]);
            } catch (\Exception $e) {
                json_response(0,$e->getError());
            }
            if(BannerModel::update($data,['id'=>$data['id']])) json_response(1,'操作成功');
            json_response(0,'操作失败');
    }

    /**
     * 删除banner
     * date 2020/8/13 17:55
     */
    public function delete() {
        $data = request()->post();
        try {
            $this->validate($data, [
                'ids' => 'require',
            ], [
                'ids.require'  => 'ID必须',
            ]);
        } catch (Exception $e) {
            json_response(0,$e->getError());
        }
        $ids = explode(',',$data['ids']);
        if(!$banner = BannerModel::where('id','in',$ids)->select()) json_response(0,'不存在');
        if($banner->delete()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    
}